import request from 'utils/request';

export interface CustomerItem {
  id: string;
  company_name: string;
  invite_code: string;
  admin_name: string;
  admin_phone: string;
  company_account_balance: number;
  company_account_balance_warning_val?: number;
  company_account_balance_warning_phone?: string;
  company_account_balance_warning_enable?: boolean;
  operator_type?: string;
  created_at: string;
  updated_at: string;
}

export interface CustomerListParams {
  page?: number; // 默认 1
  page_size?: number; // 默认 20
  search?: string; // 组合搜索：企业名 管理人姓名 手机号，用空格拼接
  sort_by?: 'created_at' | 'updated_at';
  sort_order?: 'asc' | 'desc';
}

export interface CustomerListResult {
  code: string | number;
  msg?: string;
  data: {
    items: CustomerItem[];
    total: number;
    page: number;
    page_size: number;
    total_pages: number;
  };
}

// 获取客户列表（GET /api/v1/customers）
export const getCustomerList = async (params: CustomerListParams = {}) => {
  const {
    page = 1,
    page_size = 20,
    search = '',
    sort_by = 'created_at',
    sort_order = 'desc',
  } = params;

  const result = await request.get<CustomerListResult>('/api/v1/customers', {
    params: { page, page_size, search, sort_by, sort_order },
  });
  return result;
};

// 获取客户详情（GET /api/v1/customers/{customer_id}）
export interface CustomerDetailResult {
  code: string | number;
  msg?: string;
  data: CustomerItem;
}

export const getCustomerDetail = async (customer_id: string) => {
  const result = await request.get<CustomerDetailResult>(`/api/v1/customers/${customer_id}`);
  return result;
};

// 删除客户（DELETE /api/v1/customers/{customer_id}）
export interface DeleteCustomerResult {
  code: string | number;
  msg?: string;
  data: null;
}

export const deleteCustomer = async (customer_id: string) => {
  const result = await request.delete<DeleteCustomerResult>(`/api/v1/customers/${customer_id}`);
  return result;
};

// 更新客户信息（PUT /api/v1/customers/{customer_id}）
export interface UpdateCustomerBody {
  company_name?: string;
  admin_name?: string;
  admin_phone?: string;
  company_account_balance_warning_val?: number;
  company_account_balance_warning_phone?: string;
  company_account_balance_warning_enable?: boolean;
}

export interface UpdateCustomerResult {
  code: string | number;
  msg?: string;
  data: CustomerItem;
}

export const updateCustomer = async (customer_id: string, body: UpdateCustomerBody) => {
  const result = await request.put<UpdateCustomerResult>(`/api/v1/customers/${customer_id}`, body);
  return result;
};

// 更新客户余额（PATCH /api/v1/customers/{customer_id}/balance）
export type BalanceOperation = 'add' | 'subtract';

export interface UpdateCustomerBalanceBody {
  amount: number; // 单位：分
  operation: BalanceOperation;
  remark?: string;
}

export interface UpdateCustomerBalanceResult {
  code: string | number;
  msg?: string;
  data: {
    customer_id: string;
    old_balance: number;
    new_balance: number;
    amount: number;
    operation: BalanceOperation;
    remark?: string;
    updated_at: string;
  };
}

export const updateCustomerBalance = async (
  customer_id: string,
  body: UpdateCustomerBalanceBody
) => {
  const result = await request.patch<UpdateCustomerBalanceResult>(
    `/api/v1/customers/${customer_id}/balance`,
    body
  );
  return result;
};

// 创建客户（POST /api/v1/customers）
export interface CreateCustomerBody {
  company_name: string;
  admin_name: string;
  admin_phone: string;
  admin_password: string;
  operator_type?: 'CLIENT' | 'PLATFORM';
  // 下列字段由后端给默认值，如前端不传即可；如需传值，可在调用处补充
  company_account_balance_warning_val?: number;
  company_account_balance_warning_phone?: string;
  company_account_balance_warning_enable?: boolean;
}

export interface CreateCustomerResult {
  code: string | number;
  msg?: string;
  data: CustomerItem;
}

export const createCustomer = async (body: CreateCustomerBody) => {
  const result = await request.post<CreateCustomerResult>('/api/v1/customers', body);
  return result;
};
